const queryString = require('querystring');
const server = require('http');
const url = require('url');
const fs = require('fs');
const path = require('path');
server.createServer((request, response) => {
    let urlObj = url.parse(request.url);
    let readStream = fs.createReadStream(path.join(__dirname, 'login.html'));
    response.writeHead(200,{
        'Content-Type': 'text/html; charset=utf-8'
    });
    let readContent = '';
    if(urlObj.pathname === '/') {
        readStream.on('data',(chunk) => {
            readContent += chunk;
        });
        readStream.on('end', () => {
            response.end(readContent);
        })
    }else if(urlObj.pathname === '/login') {
        if(request.method === 'POST') {
            request.on('data', (chunk) => {
                readContent += chunk;
            });
            request.on('end', () => {
                let objPase = queryString.parse(readContent);
                response.end(`用户名为:${objPase.username},密码为:${objPase.password}`);
            })
        }else{
            response.end(`你请求的方法为${request.method},不是POST`);
        }
    }
}).listen(3000, '127.0.0.1', ()=>{
    console.log('服务已经启动');
});